package com.aorise.service.organ.impl;

import com.aorise.exception.ServiceException;
import com.aorise.mapper.organ.OrganMapper;
import com.aorise.mapper.system.UserMapper;
import com.aorise.service.organ.OrganService;
import com.aorise.model.organ.*;
import com.aorise.utils.ConstDefine;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:cat
 * @Desicription: 组织架构接口服务实现类
 * @Date:Created in 2019-05-23
 * @Modified By:
 */
@Service
public class OrganServiceImpl implements OrganService {

    @Autowired
    private OrganMapper organMapper;
    @Autowired
    private UserMapper userMapper;

    List<OrganModel> listTemp;
    /**
     * @Author:cat
     * @Description 查询所有组织架构信息
     * @Date:2019-05-23
     * @Return: List<OrganModel> 组织架构信息集合
     * @Modified By:
     */
    @Override
    public List<OrganModel> getAllOrganList() {
        listTemp = new ArrayList<>();
        getChildren(0,1);

        return listTemp;
    }

    /**
     * 递归查询组织架构信息
     * @param parentId 父级ID
     * @param level 层级
     * @return
     */
    public void getChildren(Integer parentId,Integer level) {

        Map<String, Object> map = new HashMap<>();
        map.put("parentId", parentId);
        map.put("state", ConstDefine.STATE_ABLE);
        List<OrganModel> children = organMapper.getOrganByParentId(map);
        for (OrganModel organModel : children) {
            OrganModel model = new OrganModel();
            model.setId(organModel.getId());
            model.setOName(organModel.getOName());
            model.setParentId(organModel.getParentId());
            model.setLevel(level);

            listTemp.add(model);

            getChildren(organModel.getId(), level +1);
        }
    }

    /**
     * @Author:cat
     * @Description 按层级查询所有组织架构信息
     * @Date:2019-05-23
     * @Return: List<OrganModel> 组织架构信息集合
     * @Modified By:
     */
    @Override
    public List<OrganModel> getAllOrganListTree() {

        return getChildrenTree(0,1);
    }

    /**
     * 递归查询组织架构信息（树形）
     * @param parentId 父级ID
     * @param level 层级
     * @return
     */
    public List<OrganModel> getChildrenTree(Integer parentId,Integer level) {
        List<OrganModel> list = new ArrayList<>();

        Map<String, Object> map = new HashMap<>();
        map.put("parentId", parentId);
        map.put("state", ConstDefine.STATE_ABLE);
        List<OrganModel> children = organMapper.getOrganByParentId(map);
        for (OrganModel organModel : children) {
            OrganModel model = new OrganModel();
            model.setId(organModel.getId());
            model.setOName(organModel.getOName());
            model.setParentId(organModel.getParentId());
            model.setLevel(level);
            model.setOrganModels(getChildrenTree(organModel.getId(),level+1));
            list.add(model);
        }
        return list;
    }

    /**
     * @Author:cat
     * @Description 根据ID查询组织架构详情
     * @params: id 主键ID
     * @Date:2019-05-23
     * @Return: OrganModel 组织架构信息
     * @Modified By:
     */
    @Override
    public OrganModel getOrganById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("state", ConstDefine.STATE_ABLE);
        OrganModel organModel = organMapper.getOrganById(map);
        if(!organModel.getParentId().toString().equals("0")){
            map.put("id", organModel.getParentId());
            OrganModel model = organMapper.getOrganById(map);
            organModel.setParentParentId(model.getParentId());
        }
        return organModel;
    }

    /**
     * @Author:cat
     * @Description 新增组织架构信息
     * @params: organAddVO 组织架构信息
     * @Date:2019-05-23
     * @Return: Integer 主键
     * @Modified By:
     */
    @Override
    public Integer addOrgan(OrganAddVO organAddVO) {
        Integer iRet = organMapper.addOrgan(organAddVO);
        if(iRet>0){
            return organAddVO.getId();
        }else {
            return 0;
        }
    }

    /**
     * @Author:cat
     * @Description 修改组织架构信息
     * @params: organEditVO 组织架构信息
     * @Date:2019-05-23
     * @Return: Integer 影响行数
     * @Modified By:
     */
    @Override
    public Integer updateOrgan(OrganEditVO organEditVO) {
        organEditVO.setState(ConstDefine.STATE_ABLE);
        return organMapper.updateOrgan(organEditVO);
    }

    /**
     * @Author:cat
     * @Description 删除组织架构信息
     * @params: map 查询条件Map
     * @Date:2019-05-23
     * @Return: Integer 影响行数
     * @Modified By:
     */
    @Override
    @Transactional
    public Integer deleteOrgan(Map<String, Object> map) {
        /**验证是否有子级组织*/
        Map<String, Object> mapO = new HashMap<>();
        mapO.put("parentId", map.get("id"));
        mapO.put("state", ConstDefine.STATE_ABLE);
        List<OrganModel> children = organMapper.getOrganByParentId(mapO);
        if (children.size()>0){
            throw new ServiceException(StatusDefineMessage.GetMessage(StatusDefine.ORGAN_EXIST));
        }

        int iRet = organMapper.deleteOrgan(map);
        if (iRet>0) {
            /**同时解除用户的关联组织*/
            Map<String, Object> mapU = new HashMap<>();
            mapU.put("orgId",map.get("id"));
            mapU.put("editUser", map.get("editUser"));
            mapU.put("state", ConstDefine.STATE_ABLE);
            userMapper.updateUserOrganId(mapU);
        }
        return iRet;
    }

}
